<!DOCTYPE html>
<html>
<head>
    <meta name="author" content="ginko"/>
    <meta charset="utf-8"/>
    <meta http-equiv="content-type" content='text/html;charset=UTF-8'/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="msapplication-tap-highlight" content="no"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="format-detection" content="telephone=no">
    <style>
        html, body {
            padding: 0;
            margin: 0;
            font-size: 16px;
        }

        body:after {
            content: "power by ginko.wang    mailTo:yingosen@gmail.com";
            position: absolute;
            bottom: 2rem;
            right: 5rem;
            text-shadow: 0 0 .25rem #2e2e2e;
        }

        .echarts {
            position: absolute;
            width: 100%;
            height: 100%;
        }

    </style>
</head>
<body class="body">
<div id="main" class="main echarts"></div>
<section>
    <script charset="utf-8" type="text/javascript" src="./js/jquery.js?v=0a83d"></script>
    <script charset="utf-8" type="text/javascript" src="./js/jquery.mobile.custom.min.js?v=0a83d"></script>
    <script charset="utf-8" type="text/javascript" src="./js/Logger.js?v=0a83d"></script>
    <script charset="utf-8" type="text/javascript" src="./js/echarts.min.js?v=0a83d"></script>
    <script charset="utf-8" type="text/javascript" src="./js/itit.common.js?v=0a83d"></script>
    <script charset="utf-8" type="text/javascript" src="./js/itit.map.js?v=0a83d"></script>
    <script>
        $(function () {
            function randomData() {
                return Math.round(Math.random() * 1000);
            }

            function randomColor() {
                return "rgba(" + Math.round(Math.random() * 200) + ","
                        + Math.round(Math.random() * 200) + ","
                        + Math.round(Math.random() * 200) + ",.3)";
            }

            var interval = 0;
            var randomZoom = function (chart, features) {
                window.clearInterval(interval);
                var locations = [];
                for (var i = 0; i < features.length; i++) {
                    var feature = features[i];
                    var properties = feature.properties;
                    locations.push({
                        name: properties.name,
                        cp: properties.cp,
                        itemStyle: {
                            normal: {
                                areaColor: randomColor(),
                                shadowColor: "rgba(0,0,0.3)"
                            }
                        },
                        selected: false
                    });
                }
                interval = window.setInterval(function () {
                    (function (chart, locations) {
                        var _len = locations.length;
                        for (var i = 0; i < _len; i++) {
                            locations[i].selected = false;
                        }
                        var _idx = parseInt(Math.floor((Math.random() * _len)));
                        var _randomItem = locations[_idx];
                        _randomItem.selected = true;
                        chart.setOption({
                            series: [{
                                center: _randomItem.cp,
                                zoom: 3,
                                data: locations,
                                animationDurationUpdate: 1000,
                                animationEasingUpdate: 'cubicInOut'
                            }]
                        });
                    })(chart, locations);
                }, 5000);
            };
            var updateMapView = function (chart, map) {
                chart.showLoading();
                if (!map) {
                    alert("未找到对应地图信息");
                    chart.hideLoading();
                    return;
                }
                $.get("./" + map.json, function (geoJson) {
                    chart.hideLoading();
                    echarts.registerMap(map.code, geoJson);
                    var features = geoJson.features;
                    var data = [];
                    var defines = [];
                    for (var i = 0; i < features.length; i++) {
                        var feature = features[i];
                        data.push({
                            name: feature.properties.name,
                            itemStyle: {
                                normal: {
                                    areaColor: randomColor(),
                                    shadowColor: "rgba(0,0,0.3)"
                                }
                            }
                        });
                        defines.push({
                            name: feature.properties.name,
                            code: feature.id,
                            json: "/map/geo/" + feature.id + ".json"
                        });
                    }
                    chart.setOption({
                        selectedMode: "single",
                        toolbox: {
                            show: true,
                            feature: {
                                restore: {show: true},
                                saveAsImage: {show: true},
                            }
                        },
                        tooltip: {
                            trigger: 'item',
                            formatter: '{b}'
                        },
                        series: [{
                            type: "map",
                            mapType: map.code,
                            roam: true,
                            label: {
                                normal: {
                                    show: true
                                },
                                emphasis: {
                                    show: true
                                }
                            },
                            data: data
                        }]
                    });
                    randomZoom(chart, features);
                });
                (function () {
                    setTimeout(function () {
                        mapChart.resize();
                    }, 0)
                })();
            };
            var mapChart = echarts.init(document.getElementById('main'));
            updateMapView(mapChart, itit.map.getByCode("china"));
            mapChart.on("mapselectchanged", function (data) {
                if ($("#main").hasClass("taphold")) {
                    return;
                }
                var _batch = data.batch;
                if(!Array.isArray(_batch) || _batch.length === 0){
                    return;
                }
                var _data = _batch[0];
                updateMapView(mapChart, itit.map.getByName(_data.name));
            });
            $("body").on("tap", "#main", function () {
                var event = arguments[0] || window.event;
                event.preventDefault();
                event.stopPropagation();
                $(this).removeClass("taphold");
            }).on("taphold", "#main", function () {
                var event = arguments[0] || window.event;
                event.preventDefault();
                event.stopPropagation();
                $(this).addClass("taphold");
            });
            $(window).on("resize", function () {
                var event = arguments[0] || window.event;
                event.preventDefault();
                (function () {
                    setTimeout(function () {
                        mapChart.resize();
                    }, 0)
                })();
            })
        });
    </script>
</section>
</body>
</html>